<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:01
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\business;

use app\common\lib\Status;
use app\common\validate\System as ValObj;
use app\common\model\mysql\System as ModelObj;
use think\Exception;
use think\facade\Cache;

class System extends Base
{
    /**
     * @var ValObj
     */
    protected ValObj $valObj;
    /**
     * @var ModelObj
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }

    /**
     * @param array $data
     * @return bool
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(array $data = []): bool
    {
        //验证字段
        $this->isValidateAdd($data);
        //添加入库
        $result = $this->modelObj->getSystemByData($data);
        if($result){
            Cache::delete(Status::CacheSystemCodeKey);
            return true;
        }else{
            throw new Exception("添加失败");
        }
    }
    public function getSystemById(){
        $getSystem = Cache::get(Status::CacheSystemCodeKey);
        if($getSystem){
            return $getSystem;
        }else{
            $system = $this->modelObj->getSystemById();
            if($system){
                $system = $system->toArray();
                Cache::set(Status::CacheSystemCodeKey,$system);
                return $system;
            }else{
                return [];
            }
        }

    }
}